const query = require('../../db/db')
const moment = require('moment')
//上传文章内容
exports.uploadContent = async (req, res) => {
    try {
        let email = req.auth.email//用户邮箱
        let content = req.body.content//文章内容html格式
        let title = req.body.title//标题
        let introduce = req.body.introduce//摘要
        let writeType = req.body.writeType//文章类型
        /** 
         *   1 原创
         *   2 转载
         *   3 翻译
         */
        let publishType = req.body.publishType//发布形式
        /**
         * 1 全部可见
         * 2 本人可见
         * 3 粉丝可见
         * 4 非屏蔽可见
         */
        let createtime = moment().format('YYYY-MM-DD HH:mm:ss')//发布时间
        let filesurl = req.body.filesurl//封面图片路径
        let showLabels = req.body.showLabels//用户选择的标签
        let cityName = req.body.cityName//城市名
        let isBigArticle = req.body.isBigArticle//是否是长篇文章类型 凡是编辑器所写介属于长文章
        //对数据库操作
        const sql = 'insert into write_article set ?'
        console.log("封面图片路径", filesurl);
        let data = { email, content, title, introduce, writeType, publishType, createtime, filesurl, showLabels, cityName, isBigArticle }
        const result = await query(sql, data)
        console.log(result);
        if (result.affectedRows == 1) {
            return res.send({
                status: 0,
                data: data,
            })
        }
    } catch (error) {
        res.cc(error)
    }
}
//获取指定文章
exports.getArticle = async (req, res) => {
    try {
        const email = req.query.email
        const createtime = req.query.createtime
        console.log("email", email);
        console.log("createtime", createtime);
        const sql = 'SELECT * FROM write_article WHERE email=? and createtime=?'
        const sqls = 'SELECT * FROM write_article LEFT JOIN ev_users_email ON write_article.email = ev_users_email.email WHERE write_article.email=? and write_article.createtime=?'
        const result = await query(sqls, [email, createtime])
        console.log("email", result);
        if (result.length >= 1) {
            return res.send({
                status: 0,
                data: result,
            })
        } else {
            return res.send({
                status: 1,
                data: '暂无数据',
            })
        }
    } catch (error) {
        console.log(error);
        res.cc(error)
    }
}

//获取全部上传文章内容
exports.getUploadContent = async (req, res) => {
    try {
        const sql = 'SELECT write_article.*,ev_users_email.files FROM write_article LEFT JOIN ev_users_email ON write_article.email=ev_users_email.email'
        const result = await query(sql)
        console.log("asdasd", result);
        if (result.length >= 1) {
            return res.send({
                status: 0,
                data: result,
            })
        } else {
            return res.send({
                status: 1,
                data: '暂无数据',
            })
        }
    } catch (error) {
        res.cc(error)
    }
}


